﻿using System;
using System.Web;
using System.Configuration;
using System.DirectoryServices;

namespace Shuba.Web
{
    public partial class reset_pwd : basePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Form.DefaultFocus = txtCurrentPass.ClientID;
            }
        }

        public bool ResetPassword(string NIP, string oldPass, string newPass)
        {
            bool res = false;
            string LDAPServer = ConfigurationManager.AppSettings["ldap"];
            DirectoryEntry uEntry = new DirectoryEntry(LDAPServer, NIP, oldPass, AuthenticationTypes.Secure);

            try
            {
                DirectoryEntry de = uEntry;
                DirectorySearcher search = new DirectorySearcher(de);
                search.Filter = "(SAMAccountName=" + NIP + ")";
                SearchResult result = search.FindOne();

                DirectoryEntry updateEntry = result.GetDirectoryEntry();

                updateEntry.Invoke("ChangePassword", new object[] { oldPass, newPass });
                updateEntry.CommitChanges();
                updateEntry.Close();

                search.Dispose();
                res = true;
            }
            catch (Exception ex) { Alert(ex.InnerException.Message); }
            return res;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string Err = "";
            bool IsLDAPUSer = CheckLDAPUser(HttpContext.Current.User.Identity.Name, txtCurrentPass.Text);
            if (Err == "")
            {
                if (!IsLDAPUSer)
                {
                    Err = "Anda salah mengisi Password Lama";
                    Alert(Err);
                }
            }
            if (Err == "")
            {
                if (txtCurrentPass.Text.Trim().ToLower().Equals(txtPassword.Text.Trim().ToLower()))
                {
                    Err = "Password Baru harus berbeda dengan Password Lama";
                    Alert(Err);
                }
            }
            if (Err == "")
            {
                if (!txtPassword.Text.Trim().ToLower().Equals(txtRepeat.Text.Trim().ToLower()))
                {
                    Err = "Konfirmasi Password berbeda dengan Password Baru\\nSilakan ulangin kembali";
                    Alert(Err);
                }
            }           
            if (Err == "")
            {
                if (ResetPassword(HttpContext.Current.User.Identity.Name, txtCurrentPass.Text.Trim(), txtPassword.Text.Trim()))
                {
                    AlertAndRedirect("Password NT Anda sukses diganti\\nSilakan login dengan password NT yang baru", "login.aspx");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("login.aspx");
        }
    }
}
